package com.dsunsoft.rtu.service.impl;

import com.dsunsoft.rtu.common.vo.NgTreeVO;
import com.dsunsoft.rtu.core.base.BaseServiceImpl;
import com.dsunsoft.rtu.dao.SysMenuMapper;
import com.dsunsoft.rtu.dto.SysMenuTreeDto;
import com.dsunsoft.rtu.entity.SysMenu;
import com.dsunsoft.rtu.query.SysMenuQuery;
import com.dsunsoft.rtu.service.SysMenuService;
import org.springframework.stereotype.Service;

import java.util.ArrayList;
import java.util.List;
import java.util.stream.Collectors;

@Service
public class SysMenuServiceImpl extends BaseServiceImpl<SysMenuMapper, SysMenu, SysMenuQuery> implements SysMenuService {
    private static String FA_FOLDER = "ui-icon-folder";
    // 扩展图标
    private static String FA_FOLDER_OPEN = "ui-icon-folder-open";


    @Override
    public List<SysMenuTreeDto> selectListTree(SysMenuQuery query) {
        ArrayList<SysMenuTreeDto> listTree = new ArrayList<SysMenuTreeDto>();
        List<SysMenu> listAll = mapper.selectList(query);
        if (listAll != null) {
            List<SysMenu> first = listAll.stream().filter(i -> i.getLevel() == 1).collect(Collectors.toList());
            for (SysMenu menu : first) {
                SysMenuTreeDto newTree = new SysMenuTreeDto();
                newTree.setLabel(menu.getMenuName());
                newTree.setData(menu);
                newTree.setCollapsedIcon(FA_FOLDER);
                newTree.setExpandedIcon(FA_FOLDER_OPEN);
                newTree.setExpanded(false);
                listTree.add(newTree);

                procTree(listAll, menu.getMenuId(), newTree);
            }
        }
        return listTree;
    }

    @Override
    public List<NgTreeVO> selectNzTree(SysMenuQuery query) {
        List<NgTreeVO> listMenuTree = new ArrayList<NgTreeVO>();
        ArrayList<NgTreeVO> listTree = new ArrayList<NgTreeVO>();
        NgTreeVO tree = new NgTreeVO();
        tree.setChildren(listTree);
        tree.setTitle("功能菜单");
        tree.setExpanded(true);
        tree.setKey("rootKey");
        listMenuTree.add(tree);
        List<SysMenu> listAll = mapper.selectList(query);
        if (listAll != null) {
            List<SysMenu> first = listAll.stream().filter(i -> i.getLevel() == 1).collect(Collectors.toList());

            for (SysMenu menu : first) {
                NgTreeVO newTree = new NgTreeVO();
                newTree.setKey(menu.getMenuId());
                newTree.setTitle(menu.getMenuName());
                newTree.setExpanded(false);
                newTree.setOrigin(menu);
                listTree.add(newTree);
                procTree(listAll, menu.getMenuId(), newTree);
            }

        }
        return listMenuTree;
    }


    private void procTree(List<SysMenu> lst, String id, SysMenuTreeDto tree) {
        List<SysMenu> list = lst.stream().filter(i -> i.getPid().equals(id)).collect(Collectors.toList());
        ArrayList<SysMenuTreeDto> listChildren = new ArrayList<SysMenuTreeDto>();
        for (SysMenu sysMenu : list) {
            SysMenuTreeDto newTree = new SysMenuTreeDto();
            newTree.setLabel(sysMenu.getMenuName());
            newTree.setData(sysMenu);
            newTree.setCollapsedIcon(FA_FOLDER);
            newTree.setExpandedIcon(FA_FOLDER_OPEN);
            newTree.setExpanded(false);
            listChildren.add(newTree);

            procTree(lst, sysMenu.getMenuId(), newTree);
        }
        tree.setChildren(listChildren);
    }


    private void procTree(List<SysMenu> lst, String id, NgTreeVO tree) {
        List<SysMenu> list = lst.stream().filter(i -> i.getPid().equals(id)).collect(Collectors.toList());
        ArrayList<NgTreeVO> listChildren = new ArrayList<NgTreeVO>();
        if (list != null && list.size() > 0) {
            for (SysMenu sysMenu : list) {
                NgTreeVO newTree = new NgTreeVO();
                newTree.setTitle(sysMenu.getMenuName());
                newTree.setKey(sysMenu.getMenuId());
                newTree.setExpanded(false);
                newTree.setOrigin(sysMenu);
                listChildren.add(newTree);
                procTree(lst, sysMenu.getMenuId(), newTree);
            }
        } else {
            tree.setIsLeaf(true);
        }
        tree.setChildren(listChildren);
    }
}
